package com.zm.demo.core.file;


import org.junit.Test;

import java.io.*;
import java.util.*;

/**
 * @author z01140
 * @Package: com.zm.demo.core.file
 * @Description:
 * @CreateDate 2022/2/21 14:58
 * @UpdateDate 2022/2/21 14:58
 */
public class FileReadTest1 {


    @Test
    public void  test() throws IOException {

        File f = new File("D:\\zm\\工作\\06\\user-agents_macosx.txt");
        BufferedReader br = null;
        try {
            br = new BufferedReader(new FileReader(f));
            String line;
            Map<String, Set<String>> map = new HashMap<>();
            Set<String> versionSets = new TreeSet<>();
            while ((line = br.readLine()) != null){
                String []split;
                if (line != null && (split = line.split("\\)")).length > 0){
                    if (split[0].indexOf("Macintosh; Intel Mac OS X") != -1){
                        String head[] = split[0].split(" ");
                        if (head != null && head.length>0){
                            String v = head[head.length-1];
                            if (v.indexOf("10_") != -1 && v.split("_").length>2){

                                map.compute(v.substring(0, v.lastIndexOf("_")).replaceAll("_", "\\."), (a,b) -> {
                                    if (b == null){
                                        b = new TreeSet<>();
                                    }
                                    b.add(v);
                                    return b;
                                });
                            }
                        }
                    }
                }
            }
            System.out.println();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (br != null){
                br.close();
            }
        }
    }

    /**
     * +--------+------------+----------------+------------------------+----------------+
     * | x_axis | _measure_0 | p_cash_is_over | p_register_date_bucket | p_progress_ind |
     * +--------+------------+----------------+------------------------+----------------+
     * @return
     */
    public List<Map<String,Object>> testImageInfo() {
        File f = new File("D:\\zm\\工作\\02\\统一分析\\1644894515174-381423-数据库查询结果.txt");
        BufferedReader br = null;
        try {
            br = new BufferedReader(new FileReader(f));
            String line = "";
            List<Map<String,Object>> lineList = new ArrayList<>();
            while ((line = br.readLine()) != null){
                String []split = new String[]{};
                if (line != null && (split = line.split("\\|")).length > 1){
                    Map<String,Object> l = new HashMap<>();
                    l.put("x_axis", "NULL".equalsIgnoreCase(split[1].trim())? null : split[1].trim());
                    l.put("_measure_0", "NULL".equalsIgnoreCase(split[2].trim())? 0 : Integer.valueOf(split[2].trim()));
                    l.put("p_cash_is_over", "NULL".equalsIgnoreCase(split[3].trim())? null : split[3].trim());
                    l.put("p_register_date_bucket", "NULL".equalsIgnoreCase(split[4].trim())? null : split[4].trim());
                    l.put("p_progress_ind", "NULL".equalsIgnoreCase(split[5].trim())? null : split[5].trim());
                    lineList.add(l);
                }
            }
            System.out.println();
            return lineList;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (br != null){
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }


    public static void main(String[] args) {
        new FileReadTest1().readFile("C:\\Users\\34543\\IdeaProjects\\wms\\cbe-wms-core\\src\\main\\java\\com\\samton\\erp\\api",
                Arrays.asList("inventory","fba","inventoryAdjustment","reimbursement","ship", "storage", "shipment", "ledgerDetail","ledgerSummary","replenish","warehouse"), "D:\\zm\\工作\\11\\跨境电商");
    }

//    读取包下所有类的import 行，并过滤出指定结构下的数据

    public void readFile(String basePath, List<String> subFile, String outFileStr){

        File[]childFiles = new File(basePath).listFiles();

        SortedSet<String> set = new TreeSet<>();
        if (childFiles != null && childFiles.length > 0){
            for (File childFile : childFiles) {
                //不是指定包，则跳过
                if (!subFile.contains(childFile.getName())){
                    continue;
                }
                if (childFile.isDirectory()){
                    processFile(childFile, subFile, set);
                }else {
                    doProcessFile(childFile, subFile, set);
                }
            }
        }

        StringBuilder serviceString = new StringBuilder("类名,依赖包");
        StringBuilder mapperString = new StringBuilder("类名,依赖包");
        if (set.size()>0){
            boolean first = true;
            for (String s : set) {
                if (s.contains("Mapper")){
                    if (mapperString.length() > 0){
                        mapperString.append("\n").append(s);
                    }else {
                        mapperString.append(s);
                    }
                }else {
                    if (serviceString.length() > 0){
                        serviceString.append("\n").append(s);
                    }else {
                        serviceString.append(s);
                    }
                }
            }
        }

        try (BufferedWriter writer = new BufferedWriter(new FileWriter( new File(outFileStr + "\\service.csv")))){
            System.out.println(serviceString);
            writer.append(serviceString.toString());
            writer.flush();
        }catch (Exception e){
            e.printStackTrace();
        }

        try (BufferedWriter writer = new BufferedWriter(new FileWriter( new File(outFileStr + "\\mapper.csv")))){
            System.out.println(serviceString);
            writer.append(serviceString.toString());
            writer.flush();
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    private void processFile(File file, List<String> subFile, SortedSet<String> set){
        File[]childFiles = file.listFiles();
        if (childFiles != null && childFiles.length > 0){
            for (File childFile : childFiles) {
                if (childFile.isDirectory()){
                    processFile(childFile, subFile, set);
                }else {
                    doProcessFile(childFile, subFile, set);
                }
            }
        }
    }

    //已经是一个文件
    private void doProcessFile(File file, List<String> subFile, SortedSet<String> set){
        try(BufferedReader reader = new BufferedReader(new FileReader(file))){
            String line ="";
            while ((line = reader.readLine()) != null){
//                System.out.println(line);
                if (line.contains("import") && line.contains("com.samton.erp.api") && contains(line, subFile)){
                    set.add(file.getName() + "," + line.replace("import ", ""));
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }


    public boolean contains(String line, List<String> subFile){

        for (String s : line.split("\\.")) {
            if (subFile.contains(s)){
                return false;
            }
        }

        List<String> pojoList = Arrays.asList("dto", "entity","vo","constant","enum","enums", "utils", "util","bean","annotations");
        for (String s : line.split("\\.")) {
            if (pojoList.contains(s)){
                return false;
            }
        }

        return true;
    }

}
